package cn.hl.sfzy.controller;


import cn.hl.sfzy.entity.Systemuser;
import cn.hl.sfzy.entity.User;
import cn.hl.sfzy.service.*;
import cn.hl.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TOOO
 * @Author Lilysi
 * @Date 2024//10//24//11:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/User")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private UserMarkService userMarkService;
    @Autowired
    private ConsultReplyService consultReplyService;
    @GetMapping
    public Result pageSelect(@RequestParam(required = false,defaultValue = "1") int pageNum, @RequestParam(required = false,defaultValue = "5") int pageSize, User user) {
        PageInfo<User> byPage = userService.findByPage(pageNum, pageSize, user);

        return Result.success(byPage);
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User users = userService.getUser(user);
        if (users == null) {
            return Result.error("用户名或密码错误");
        }
        if (users.getPassword()==null) {
            return Result.error("用户名或密码错误");
        }
        return Result.success(users);

    }
    @GetMapping("/findAll")
    public Result findAll(){
        return Result.success(userService.findAll(new User()));
    }
    @PostMapping
    public Result insert(@RequestBody User user) {
        int insert = userService.insert(user);
        return Result.success(insert);
    }


    @PutMapping
    public Result update(@RequestBody User user) {
        int update = userService.update(user);
        return Result.success(update);
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteByUser_id(@PathVariable Long id) {
        appointmentService.deleteByUserid(id);
        userMarkService.deleteByUserId(id);
        consultReplyService.deleteByUserId(id);
        userService.delete(id);
        return Result.success("删除成功");
    }
}
